package com.njtcedu.microblog.intranet.systemconfig.repository;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;
import com.njtcedu.microblog.common.entity.dictionary.AdvertisingImage;
import com.njtcedu.microblog.common.exception.BusinessException;
import com.njtcedu.microblog.common.repository.AbstractRepository;
import com.njtcedu.microblog.common.repository.CompressImage;
import com.njtcedu.microblog.common.repository.RowMapper;
import com.njtcedu.microblog.common.util.StringUtils;
import com.njtcedu.microblog.intranet.systemconfig.web.request.AdImageRequest;
import com.njtcedu.microblog.intranet.systemconfig.web.request.AdImagesRequest;
import com.njtcedu.microblog.intranet.systemconfig.web.response.AdImageResponse;

@Repository
public class SystemConfigRepository extends AbstractRepository {

	@Value("${upload.path}")
	private String rootPath; // 暂时不启用文件系统

	public AdImageResponse uploadAdImage(MultipartFile imageFile, String evePath, String imageName) throws IOException {
			
		String fileName ="/upload/adimage/" + UUID.randomUUID().toString() + imageFile.getOriginalFilename().substring(imageFile.getOriginalFilename().lastIndexOf("."));
		String targetPath = evePath + fileName;
		InputStream uploadStream = null;
		OutputStream targetStream = null;
		AdImageResponse response = new AdImageResponse();
		response.setImageUrl(fileName);		
		try {
			uploadStream = imageFile.getInputStream();
			targetStream = new FileOutputStream(targetPath);
			byte[] cache = new byte[50];
			int count = -1;
			while ((count = uploadStream.read(cache)) != -1) {
				targetStream.write(cache, 0, count);
				targetStream.flush();
			}
		} catch (Exception e) {
			throw new BusinessException("文件写入失败.");
		} finally {
			if (null != targetStream)
				targetStream.close();
			if (null != uploadStream)
				uploadStream.close();
		}
		
		CompressImage compressImage = CompressImage.getInstance();
		compressImage.compress(targetPath, targetPath, 680, 220);
		
		final String deleteImageSql = "delete from advertisingImage where image_name = ?";
		jdbcAccess.execute(deleteImageSql, imageName);
		
		AdvertisingImage advertisingImage = new AdvertisingImage();
		advertisingImage.setImageUrl(fileName);
		advertisingImage.setImageName(imageName);
		hibernateAccess.persist(advertisingImage);
		
		return response;
	}

	public void saveImageInfo(AdImagesRequest request) {
		final String deleteImageSql = "delete from advertisingImage where image_name = ?";
		jdbcAccess.execute(deleteImageSql);

		for (AdImageRequest adImageRequest : request.getImages()) {

			if (!StringUtils.hasText(adImageRequest.getImageUrl()))
				continue;
			AdvertisingImage advertisingImage = new AdvertisingImage();
			advertisingImage.setImageUrl(adImageRequest.getImageUrl());
			hibernateAccess.persist(advertisingImage);
		}
	}
	
	public List<AdImageResponse> getAdImages() {
		final String adImageSql = "select image_url as imageUrl from advertisingImage order by image_name";
		return jdbcAccess.find(adImageSql, new RowMapper<AdImageResponse>() {

			@Override
			public AdImageResponse mapRow(ResultSet resultSet, int rowNum)
					throws SQLException {
				AdImageResponse response = new AdImageResponse();
				response.setImageUrl(resultSet.getString("imageUrl"));
				return response;
			}
		});
	}
}
